require就是讀取外部檔案用的,通常是讀.js進來
主檔案myserver.js
myserver.js
const http = require('http');
const config = require('./config').config;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(config.port, config.hostname, () => {
console.log(`Server running at http://${config.hostname}:${config.port}/`);
});
讀取config.js
const config = {
hostname: '127.0.0.1',
port: '3000'
};
exports.config = config;
想對外分享這則貼文嗎?運用網址更方便呦~